package com.hotel.common.user.controller;

import cn.hutool.core.convert.NumberWithFormat;
import com.hotel.common.config.SystemConfig;
import com.hotel.common.param.RestPwd;
import com.hotel.common.param.UserAdd;
import com.hotel.common.param.UserLogin;
import com.hotel.common.util.JwtUtil;
import com.hotel.common.vo.R;
import com.hotel.common.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author mz
 * @since 2024-06-15
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;


    // 注册 - 验证码
    @RequestMapping("/register")
    public R register(HttpServletRequest request, @RequestBody UserAdd userAdd){

        return userService.register(request,userAdd);

    }

     // 账号 - 密码 登录
    @RequestMapping("/loginpass")
    public R loginpass(HttpServletRequest request,@RequestBody UserLogin userLogin){

        return userService.loginpass(request,userLogin);

    }

    // 手机号  验证码
    @RequestMapping("/logincode")
    public  R logincode(HttpServletRequest request, @RequestBody UserLogin userLogin){

        return userService.logincode(request,userLogin);
    }


    // 重置密码
    @RequestMapping("/reset")
    public R reset(@RequestBody RestPwd restPwd){

        return userService.reset(restPwd);
    }


    // 退出登录
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public R logout(HttpServletRequest request){

        String token = request.getHeader(SystemConfig.HEADER_TOKEN);

        return userService.logout(token);

    }


    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public R findUserInfoById(HttpServletRequest request) {
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token,"id")).intValue();
        return userService.findUserInfoById(id);
    }

}

